package tyk.preparedstatement;

import com.mysql.cj.jdbc.Driver;

import java.sql.*;
import java.util.Scanner;

/***
 *@title BatchInsert
 *@description <TODO description class purpose>
 *@author Tang'ya'kang
 *@VERSION 1.0.0
 *@CREATE 2023/5/4 20:36
 **/
public class BatchInsert {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        String account="",password="",nickname="";
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/javaweb?rewriteBatchedStatements=true","root","123456");
//        rewriteBatchedStatements=true
        String sql="insert into t_user (account,password,nickname) values(?,?,?);";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        long start=System.currentTimeMillis();
        for(int i = 0; i<10000; ++i){
            preparedStatement.setObject(1,"account"+i);
            preparedStatement.setObject(2,"password"+i);
            preparedStatement.setObject(3,"nickname"+i);
            preparedStatement.addBatch();
        }
        int[] resultSet=preparedStatement.executeBatch();
        long end=System.currentTimeMillis();
        System.out.println(end-start);
        preparedStatement.close();
        connection.close();
    }
}
